package com.qishao.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qishao.client.KillFeign;
import com.qishao.client.TestFeign;
import com.qishao.model.EduTeacher;
import com.qishao.query.TeacherQuery;
import com.qishao.service.EduTeacherService;
import com.qishao.service_base.error.CustomException;
import com.qishao.service_base.reponse.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xushaoqi
 * @since 2021-08-13
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduService/edu-teacher")
@CrossOrigin  //跨域注解
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private TestFeign testFeign;

    @Autowired
    private KillFeign killFeign;


    @PostMapping("test")
    public Response test(){
        Response test = testFeign.test();
        System.out.println(test);
        List<EduTeacher> teacherAll = teacherService.findTeacherAll();
        System.out.println(teacherAll);
        return test;
    }

    @PostMapping("killTest")
    public Map<String,Object> test(@RequestBody Map<String,Object> map){
        return killFeign.test(map);
    }


    @DeleteMapping("{id}")
    @ApiOperation("根据讲师Id逻辑删除讲师")
    public Response removeTeacher(@PathVariable String id) {
        boolean fals = teacherService.removeById(id);
        if (!fals) return Response.error();
        return Response.ok();
    }

    @ApiOperation("查询所有讲师")
    @GetMapping("/findTeacherAll")
    public Response findTeacherAll() {
        List<EduTeacher> teacherAll = teacherService.findTeacherAll();
        return Response.ok(teacherAll);
    }


    @ApiOperation("动态分页查询讲师")
    @PostMapping("/findAll")
    public Response findAll(@RequestBody TeacherQuery teacherQuery) {
        return Response.ok().data(teacherService.pageQuery(teacherQuery));
    }


    @ApiOperation("新增讲师")
    @PostMapping("/saveTeacher")
    public Response saveTeacher(@ApiParam(name = "teacher", value = "讲师对象", required = true)
                                @RequestBody(required = false) EduTeacher teacher) {
        boolean fals = teacherService.save(teacher);
        if (!fals) return Response.error();
        return Response.ok().data("data", teacher);
    }

    @ApiOperation("根据Id查询讲师")
    @GetMapping("{id}")
    public Response findById(@ApiParam(name = "id", value = "讲师id", required = true)
                             @PathVariable String id) {
        return Response.ok().data("data", teacherService.getById(id));
    }

    @ApiOperation("修改讲师信息")
    @PostMapping("{id}")
    public Response editTeacher(@ApiParam(name = "id", value = "讲师id", required = true)
                                @PathVariable String id,
                                @ApiParam(name = "teacher", value = "讲师对象", required = true)
                                @RequestBody EduTeacher teacher) {
        teacher.setId(id);
        teacherService.updateById(teacher);
        return Response.ok().data("data", teacher);
    }


}

